home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 04 - File Management / ResourcesDemo / ResourcesDemo.c next >
Encoding:
C/C++ Source or Header  |  1995-02-18  |  1.8 KB  |  71 lines  |  [TEXT/MMCC]

  1.  
  2. /* ResourcesDemo */
  3. /* by Ingemar Ragnemalm 1995 */
  4.  
  5. /* Demonstrates how to use custom resources from your programs. */
  6.  
  7. /* A demo program that has a resource, stored in itself (should rather be in a */
  8. /* preference file in a real program) with a single variable, myCount. */
  9. /* Each time the program is run, the resource is read, myCount is incremented, and */
  10. /* the program beeps as many times as myCount says. */
  11.  
  12.  
  13. /* Define a structure and a handle type to it. This is our resource format! */
  14.  
  15. typedef struct {
  16.     short myCount;
  17. } MyRecord;
  18. typedef MyRecord **MyHnd;
  19.  
  20.  
  21. /* Resource type and number. Use mixed-type names to avoid conflicts */
  22.  
  23. #define    kMyResType    'Demo'
  24. #define    kMyResNum    0
  25.  
  26.  
  27. MyHnd myResource;    /* The handle to our resource */
  28.  
  29. short i;            /* An integer for use as loop variable */
  30.  
  31.  
  32. static void InitToolbox()
  33. {
  34.     InitGraf(&qd.thePort);
  35.     InitCursor ();
  36.     MaxApplZone ();
  37. }; /*InitToolbox*/
  38.  
  39.  
  40. void main(void) {
  41.  
  42.     InitToolbox();
  43.  
  44. /**** Get the resource ****/
  45.  
  46.     myResource = (MyHnd)GetResource(kMyResType, kMyResNum);
  47.  
  48. /**** Create the resource if needed ****/
  49.  
  50. /* If it didn't load, we assume it doesn't exist. Create it! */
  51. /* This is done by allocating a handle, initializing the fields and calling AddResource. */
  52.     if ( myResource == 0L )
  53.         {
  54.             myResource = (MyHnd)NewHandle(sizeof(MyRecord));            /* Allocates memory */
  55.             myResource[0]->myCount = 0;                                    /* Init fields */
  56.             AddResource((Handle)myResource, kMyResType, kMyResNum, "\p");    /* Create resource */
  57.         };
  58.  
  59. /**** Change the resource ****/
  60.  
  61.     (**myResource).myCount++;        /* Change the count */
  62. /* Call ChangedResource to tell MacOS that the resource needs to be written */
  63. /* back to disk. */
  64.     ChangedResource((Handle)myResource);
  65.  
  66. /**** Use the resource. Beep as many times as myCount says. ****/
  67.     for ( i = 1 ; i <= (**myResource).myCount ; i++)
  68.         SysBeep(1);
  69.  
  70. } /* ResourcesDemo */
  71.